package first.endtoend

import grails.converters.JSON
import org.springframework.dao.DataIntegrityViolationException

class CategoryController
{

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    // def scaffold = true

    def webservice = {
        render([categorie: Category.findAll(), status: Category.findAll().size() > 0] as JSON)
    }


    def index()
    {
        redirect(action: "list", params: params)
    }

    def list(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        [categoryInstanceList: Category.list(params), categoryInstanceTotal: Category.count()]
    }

    def create()
    {
        [categoryInstance: new Category(params)]
    }

    def save()
    {
        def categoryInstance = new Category(params)
        if (!categoryInstance.save(flush: true))
        {
            render(view: "create", model: [categoryInstance: categoryInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'category.label', default: 'Category'), categoryInstance.id])
        redirect(action: "show", id: categoryInstance.id)
    }

    def show(Long id)
    {
        def categoryInstance = Category.get(id)
        if (!categoryInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'category.label', default: 'Category'), id])
            redirect(action: "list")
            return
        }
        custumRender(categoryInstance)
        //[categoryInstance: categoryInstance, productCategoryInstanceList:categoryInstance.categorieProducts, productCategoryInstanceTotal:categoryInstance.categorieProducts?.count()]
    }

    def custumRender(Category categoryInstance)
    {
        [categoryInstance: categoryInstance]
    }

    def edit(Long id)
    {
        def categoryInstance = Category.get(id)
        if (!categoryInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'category.label', default: 'Category'), id])
            redirect(action: "list")
            return
        }

        // [categoryInstance: categoryInstance]
        custumRender(categoryInstance)
    }

    def update(Long id, Long version)
    {
        def categoryInstance = Category.get(id)
        if (!categoryInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'category.label', default: 'Category'), id])
            redirect(action: "list")
            return
        }

        if (version != null)
        {
            if (categoryInstance.version > version)
            {
                categoryInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'category.label', default: 'Category')] as Object[],
                        "Another user has updated this Category while you were editing")
                render(view: "edit", model: [categoryInstance: categoryInstance])
                return
            }
        }

        categoryInstance.properties = params

        if (!categoryInstance.save(flush: true))
        {
            render(view: "edit", model: [categoryInstance: categoryInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'category.label', default: 'Category'), categoryInstance.id])
        redirect(action: "show", id: categoryInstance.id)
    }

    def delete(Long id)
    {
        def categoryInstance = Category.get(id)
        if (!categoryInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'category.label', default: 'Category'), id])
            redirect(action: "list")
            return
        }

        try
        {
            categoryInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'category.label', default: 'Category'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e)
        {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'category.label', default: 'Category'), id])
            redirect(action: "show", id: id)
        }
    }
}
